package admin

import (
	"beego/models"
	"beego/models/admin"
	"github.com/astaxie/beego"
)

type IndexController struct {
	beego.Controller
}

func (c *IndexController) Get() {
	adminId := c.GetSession("admin_id")
	//adminId := 1
	adminInfo := admin.Admin{}
	models.DB.Preload("Role").Where("id = ?", adminId).Find(&adminInfo)
	var roleSlice []interface{}
	for _, value := range adminInfo.Role {
		roleSlice = append(roleSlice, value.Id)
	}
	var roleNode []admin.RoleNode
	models.DB.Where("role_id IN (?)", roleSlice).Find(&roleNode)
	NodeAccessMap := make(map[int]int)
	for _, value := range roleNode {
		NodeAccessMap[value.NodeId] = value.NodeId
	}
	var node []admin.Node
	models.DB.Preload("NodeItem").Where("pid = ?", 0).Find(&node)

	for i := 0; i < len(node); i++ {
		if _, ok := NodeAccessMap[node[i].Id]; ok {
			node[i].Checked = true
		}
		for j := 0; j < len(node[i].NodeItem); j++ {
			if _, ok := NodeAccessMap[node[i].NodeItem[j].Id]; ok {
				node[i].NodeItem[j].Checked = true
			}
		}
	}
	//beego.Info(node)
	//c.Data["json"] = adminInfo
	//c.ServeJSON()
	c.Data["node"] = node
	c.Data["adminInfo"] = adminInfo
	c.TplName = "admin/index/index.html"
}
func (c *IndexController) Welcome() {
	adminId := c.GetSession("admin_id")
	c.Data["admin_id"] = adminId
	c.Ctx.WriteString("欢迎页面")
	//c.Ctx.WriteString("欢迎页面" + adminId)
	//c.TplName = "admin/index/index.html"
}

func (c *IndexController) Welcomes() {
	c.Ctx.WriteString("欢迎页面s")
	//c.Ctx.WriteString("欢迎页面" + adminId)
	//c.TplName = "admin/index/index.html"
}
